package aom.ju.ss.view.shua

import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ShopDetailsActivityBinding
import aom.ju.ss.databinding.ShopVideoItemBinding
import aom.ju.ss.databinding.WeibaBinding
import aom.ju.ss.mode.*
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.att
import aom.ju.ss.utils.http.LP_API.store_info
import aom.ju.ss.utils.http.LP_API.video_list
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.dialog.NavigationPopup
import aom.ju.ss.view.dialog.SharePromotionDialogLp
import com.bumptech.glide.Glide
import com.google.gson.Gson

/**
 * 店铺详情
 */
class ShopDetailsActivity : BaseActivity() {
    private lateinit var binding: ShopDetailsActivityBinding
    private lateinit var pAdapter: PAdapter
    private var list: MutableList<ShopVideoListData> = arrayListOf()
    override fun init() {
        binding = viewBinding as ShopDetailsActivityBinding

        OKHttpUtils(activity)
            .SetApiUrl(store_info)
            .SetKey("id")
            .SetValue(intent.getStringExtra("data"))
            .POST(this)
        getvs()

        pAdapter = PAdapter(
            list, R.layout.shop_video_item
        ) { bb, position ->
            val b = bb as ShopVideoItemBinding
            Glide.with(this)
                .load(list[position].image)
                .placeholder(R.drawable.default_img)
                .centerCrop()
                .into(b.img)
            b.img.setOnClickListener {
                val ls = arrayListOf<ShopVideoListData>()

                for ((i, a) in list.withIndex()) {
                    if (i == position) {
                        ls.add(a)
                    }
                }
                val js = Gson().toJson(ShopVideoList(0, 0, ls, 0, "0"))
                start(ShopVideoActivity::class.java, js)
            }
        }

        val fv = DataBindingUtil.inflate<WeibaBinding>(layoutInflater, R.layout.weiba, null, false)
        pAdapter.footerView = fv!!.rootView
        pAdapter.footerView.VG(false)

        binding.rec.layoutManager = GridLayoutManager(activity, 3)
        binding.rec.adapter = pAdapter

        binding.dianhua.setOnClickListener {
            modde?.data?.phone?.let {
                callPhone(activity!!, it)
            }
        }
        binding.daohang.setOnClickListener {
            modde?.data?.let {
                NavigationPopup(activity, it.lat.toString(), it.lng.toString())
            }
        }
        binding.titleView9.r_img_two.setOnClickListener {
            SharePromotionDialogLp(0, activity!!, null)
        }
        binding.allGoods.setOnClickListener {

        }

        binding.small.setOnRefreshListener {
            page = 1
            getvs()
        }
        binding.small.setOnLoadMoreListener {
            page++
            getvs()
        }
        binding.small.setEnableRefresh(false)
        binding.sixin.setOnClickListener { "尽请期待".show }
        binding.gz.setOnClickListener {
            OKHttpUtils(activity)
                .SetApiUrl(att)
                .SetKey("shop_id")
                .SetValue(intent.getStringExtra("data"))
                .POST(this)
        }

    }

    var page = 1
    private fun getvs() {
        OKHttpUtils(activity)
            .SetApiUrl(video_list)
            .SetKey("id", "page")
            .SetValue(intent.getStringExtra("data"), page.toString())
            .POST(this)

    }

    var modde: ShopInfo? = null
    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        if (httpTY == video_list) {
            val mode = Gson().fromJson(jsonString, ShopVideoList::class.java)
            if (page == 1) {
                list.clear()
            }
            mode?.data?.let {
                list.addAll(it)
            }
            pAdapter.notifyDataSetChanged()
            binding.small.Fin()
            pAdapter.footerView.VG(list.size > 6)
        } else if (httpTY == store_info) {
            modde = Gson().fromJson(jsonString, ShopInfo::class.java)
            binding.info = modde!!.data
            binding.textView53.juli(modde?.data?.lng, modde?.data?.lat)
        } else if (httpTY == att) {
            val m = Gson().fromJson(jsonString, PublicMode::class.java)
            m.msg?.show
            if (m.msg.equals("关注成功")) {
                modde?.data?.is_att = 1
            } else {
                modde?.data?.is_att = 0
            }
            binding.info = modde?.data

        }

    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
        binding.small.Fin()
    }

    override val lay: Int = R.layout.shop_details_activity
}